<template>
  <div>
    <div class="faultHandlingMode zy-content">
      <div class="searchBox search-box-layout">
        <Row :gutter="24">
          <Col span="4">
          <div>
            <p class="label">{{ $t("部门名称") }}</p>
            <div class="ipt">
              <Input type="text" v-model="paramsList.deptName" clearable :placeholder="`${$t('common.inputInfo')}`"
                class="inputBorder"></Input>
            </div>
          </div>
          </Col>
          <Col span="4">
          <div>
            <p class="label hide-label">--</p>
            <div class="ipt">
              <Button class="search-btn" type="primary" @click="changeNum()">{{ $t("common.Query") }}</Button>
              <Button class="reset-btn" @click="changeNum()" v-has="'dmsPurchaseList'">{{ $t("common.reset") }}
              </Button>
            </div>
          </div>
          </Col>
        </Row>
      </div>

      <div class="list-layout mt16">
        <div class="btns clear">
          <span class="title">{{ $t("common.listInformation") }}</span>
          <Button @click="editRow">
            {{ $t("common.New") }}
          </Button>
          <Button @click="getTableColumns(columns)">
            {{ handleContextMenuStatus ? $t("显示操作栏") : $t("隐藏操作栏") }}
          </Button>
        </div>
        <div class="tableContent noStripe">
          <Table ellipsis row-key="id" ref="selection" :data="dataArr" @on-selection-change="onSelectChange"
            class="resizable-table" border @on-column-width-resize="onColumnWidthResize" :columns="columnsList">
            <template slot-scope="{ row }" slot="action">
              <span class="operationBtn" @click="editRow(row)"><i class="iconfont zy-common-bianji"></i>{{
                $t("common.Edit") }}</span>
              <span class="operationBtn" @click="delRow(row)"><i class="iconfont zy-common-shanchu"></i>{{
                $t("common.Delete") }}</span>
            </template>
          </Table>
        </div>
        <!--        <div class="pageWrap mt16">
          <pageLeftTitle :total="total" :selected="selectList.length" />
          <Page :total="total"
                :page-size="pageSize"
                :page-size-opts="pageSizeOption"
                :current="pageNo"
                @on-change="changePageNum"
                @on-page-size-change="changePageSize"
                show-sizer show-total />
        </div>-->
      </div>

      <Modal :mask-closable="false" v-model="delFaultCodeShowF" :title="$t('common.Delete')" class="modal-title-layout">
        <!--        <Row class="enableContent">-->
        <!--          <Col span="24" class="enableContentInner">-->
        <!--            <img width="95px" src="~images/event/del.png" alt="">-->
        <!--          </Col>-->
        <!--          <Col span="24" class="enableTips">-->
        <!--            <span>{{$t('common.sureDelete')}}</span>-->
        <!--          </Col>-->
        <!--        </Row>-->
        <div class="icon red">
          <i class="iconfont zy-common-shanchu"></i>
        </div>
        <p class="title">
          <span>{{ $t("common.sureDelete") }}</span>
        </p>
        <div slot="footer">
          <Button type="default" @click="delFaultCodeShowF = false">{{
            $t("common.Cancle")
          }}</Button>
          <Button type="primary" @click="deleteOk">{{
            $t("maintenance.Confirmed")
          }}</Button>
        </div>
      </Modal>

      <Modal :mask-closable="false" v-model="createStatus" :title="addtitle" class="modal-content-layout">
        <Form class="formList" ref="deptFrom" :rules="ruleValidate" :model="formItem" label-position="top">
          <FormItem :label="$t('上级部门')" prop="parentId" v-if="formItem.parentId !== 0">
            <Select filterable v-model="formItem.parentId" clearable :placeholder="`${$t('common.selectInfo')}`">
              <Option v-for="item of departmentList" :value="item.id" :label="item.deptName" :key="item.id" />
            </Select>
          </FormItem>
          <FormItem :label="$t('部门名称')" prop="deptName">
            <Input type="text" v-model="formItem.deptName" :placeholder="`${$t('common.inputInfo')}`" class="inputBorder"
              :maxlength="32"></Input>
          </FormItem>
          <template v-if="addtitle === `${this.$t('common.Edit')}`">
            <FormItem :label="$t('部门主管')" prop="managerIdList">
              <Select filterable v-model="formItem.managerIdList" clearable multiple
                :placeholder="`${$t('common.selectInfo')}`">
                <Option v-for="item of managerIdListarr" :value="item.id.toString()" :label="item.realname"
                  :key="item.id" />
              </Select>
            </FormItem>
          </template>
          <FormItem :label="$t('序号')" prop="sort">
            <Input type="number" v-model="formItem.sort" :placeholder="`${$t('common.inputInfo')}`" class="inputBorder"
              :maxlength="32"></Input>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="default" @click="cancle">{{
            $t("common.Cancle")
          }}</Button>
          <Button type="primary" @click="addOk">{{
            $t("maintenance.Confirmed")
          }}</Button>
        </div>
      </Modal>
    </div>
  </div>
</template>

<script>
import {
  Input,
  Checkbox,
  Icon,
  Table,
  Page,
  Select,
  Option,
  Modal,
  Form,
  FormItem,
  InputNumber,
} from "view-design";
import toggleTableOperation from "@/mixins/toggleTableOperation";
export default {
  name: "faultHandlingMode",
  components: {
    Input,
    Checkbox,
    Icon,
    Table,
    Page,
    Select,
    Option,
    Modal,
    Form,
    FormItem,
    InputNumber,
  },
  mixins: [toggleTableOperation],
  data() {
    return {
      delFaultCodeShowF: false,
      formItem: {},
      addtitle: "",
      createStatus: false,
      pageSizeOption: [10, 20, 30],
      paramsList: {},
      pageNo: 1,
      pageSize: 10,
      total: 0,
      managerIdListarr: [],
      columns: [
        {
          // title: '部门名称',
          title: `${this.$t("部门名称")}`,
          key: "deptName",
          align: "left",
          tree: true,
          resizable: true, tooltip: true,
        },
        {
          // title: '创建人',
          title: `${this.$t("创建人")}`,
          key: "updateByName",
          align: "center",
          resizable: true, tooltip: true,
        },
        {
          // title: '创建时间',
          title: `${this.$t("创建时间")}`,
          key: "updateTime",
          align: "center",
          resizable: true, tooltip: true,
        },
        {
          title: `${this.$t("common.operation")}`,
          slot: "action",
          width: 200,
          align: "center",
          fixed: "right",
        },
      ],
      ruleValidate: {
        managerIdList: [
          {
            required: true,
            message: `${this.$t("common.selectInfo")}${this.$t("部门主管")}`,
            type: "array",
            trigger: "change",
          },
        ],
        parentId: [
          {
            required: true,
            message: `${this.$t("common.selectInfo")}${this.$t("上级部门")}`,
            type: "number",
            trigger: "change",
          },
        ],
        deptName: [
          {
            required: true,
            message: `${this.$t("common.inputInfo")}${this.$t("部门名称")}`,
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: `${this.$t("common.inputInfo")}${this.$t("序号")}`,
            trigger: "blur",
          },
        ],
      },
      departmentList: [],
      dataArr: [],
      selectList: [],
      targetRow: {},
    };
  },
  created() {
    this.getTableColumnsWidth(this.columns);
  },
  mounted() {
    this.searchList();
  },
  methods: {
    getUserNo1(row) {
      this.axios({
        method: "get",
        url: "/basic/user/findUserDetail",
        params: {
          deptId: row.deptId,
        },
      }).then((res) => {
        if (res.code === 200) {
          this.managerIdListarr = res.data;
          if (row.managerIdList) {
            this.formItem.managerIdList = row.managerIdList.split(",");
          }
          this.createStatus = true;
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
    //查询部门
    getDept(value) {
      let data = {
        deptName: value,
        pageNo: 1,
        pageSize: 1000,
      };
      this.axios({
        method: "post",
        url: "/basic/dept/getDeptSuper",
        data,
      }).then((res) => {
        if (res.code === 200) {
          this.departmentList = res.data;
        }
      });
    },
    onSelectChange(val) {
      this.selectList = val;
    },
    changeNum() {
      this.pageNo = 1;
      this.searchList();
    },
    changePageNum(val) {
      this.pageNo = val;
      this.searchList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.searchList();
    },
    editRow(row) {
      this.$refs.deptFrom.resetFields();
      this.getDept();
      this.formItem = {};
      if (row.id) {
        this.formItem = JSON.parse(JSON.stringify(row));
        this.addtitle = `${this.$t("common.Edit")}`;
        this.getUserNo1(row);
      } else {
        this.addtitle = `${this.$t("common.New")}`;
        this.createStatus = true;
      }
    },
    deleteOk() {
      let data = [];
      if (this.targetRow.id) {
        data = [this.targetRow.id];
      } else {
        if (this.selectList && this.selectList.length) {
          data = this.selectList.map((val) => val.id);
        }
      }
      this.axios({
        method: "post",
        url: "/basic/dept/deleteByIds",
        data,
      }).then((res) => {
        if (res.code === 200) {
          this.$Message.success(res.msg);
          this.changeNum();
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
        this.delFaultCodeShowF = false;
      });
    },
    delRowList() {
      if (!(this.selectList && this.selectList.length)) {
        this.$Message.warning(`${this.$t("common.deleteItems")}`);
        return;
      }
      this.delFaultCodeShowF = true;
    },
    delRow(row) {
      this.delFaultCodeShowF = true;
      this.targetRow = row;
    },
    cancle() {
      this.createStatus = false;
      this.formItem = {};
    },
    addOk() {
      this.$refs["deptFrom"].validate((valid) => {

        if (valid) {
          if (this.formItem.managerIdList && this.formItem.managerIdList.length > 0) {
            this.formItem.managerIdList =
              this.formItem.managerIdList.join(",");
          }
          const urlInfo = {
            method: "post",
            url: "/basic/dept/insertDeptSuper",
            data: {
              deptName: this.formItem.deptName,
              parentId: this.formItem.parentId,
              sort: this.formItem.sort,
              managerIdList: this.formItem.managerIdList,
              // createBy:localStorage.getItem('ziyunFactoryPortalName'),
              // createTime:this.$moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
            },
          };
          if (this.formItem.id) {
            urlInfo.url = "/basic/dept/updateDeptSuper";
            urlInfo.data.id = this.formItem.id;
            urlInfo.data.deptId = this.formItem.deptId;
            urlInfo.data.sort = this.formItem.sort;
          }

          this.axios(urlInfo).then((res) => {
            if (res.code === 200) {
              if (this.formItem.id) {
                this.$Message.success(res.msg);
              } else {
                this.$Message.success(res.msg);
              }
              this.changeNum();
              this.cancle();
            } else {
              this.$Message.warning({
                content: res.msg,
                duration: 6,
                closable: true,
              });
            }
          });
        }
      });
    },
    searchList() {
      let data = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        deptName: this.paramsList.deptName,
      };
      this.axios({
        method: "post",
        url: "/basic/dept/getDeptTree",
        params: data,
      }).then((res) => {
        if (res.code === 200) {
          this.dataArr = res.data;
          //this.total = res.data.total
        } else {
          this.$Message.warning({
            content: res.msg,
            duration: 6,
            closable: true,
          });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.faultHandlingMode {
  .formList {
    padding: 30px;

    .btn {
      margin-top: 20px;
      text-align: center;
      width: 100%;
      height: 40px;
    }

    .inputBorder {
      border: 1px solid #dcdee2;
      border-radius: 4px;
    }
  }
}
</style>

<style lang="scss"></style>
